package cc.zy.base.businesses.utils;

import com.ec.v2.config.Config;
import com.ec.v2.entity.config.*;
import com.ec.v2.entity.sales.FieldParamDTO;
import com.ec.v2.entity.sales.SaleFieldItemDTO;
import com.ec.v2.entity.sales.SalesFieldDTO;
import com.ec.v2.entity.sales.SalesResponse;
import com.ec.v2.service.Sales;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LiXu
 */
@Data
@Slf4j
public class LabelMap {

    private long corpId = 15274436L;
    private String appId = "705715394396028928";
    private String secret = "QBNKUtgRznRmTU6iEFC";

    public Map<Long, Map<String, String>> getAllLabels() throws IOException {

        // 初始化配置信息
        Config.initConfig(corpId, appId, secret);
        //得到客户自定标签集合
        ConfigResponse<ConfigCrmGroupTagResp> res = com.ec.v2.service.Config.getLabelInfo();

        // 4。 处理结果
        //存入 标签id  标签类型 标签名称
        Map<Long, Map<String, String>> labelMap = new HashMap<Long, Map<String, String>>();
        //存入 标签id  标签类型ID 标签名称
        Map<Long, Map<Long, String>> labelMapId = new HashMap<Long, Map<Long, String>>();
        List<ConfigCrmGroupTagResp> list = res.getData();
        for (ConfigCrmGroupTagResp labelGroupList : list) {
            String labelGroupName = labelGroupList.getLabelGroupName();
            log.debug(String.format("labelGroupList = %1$s", labelGroupList));
            long labegroupId = labelGroupList.getLabelGroupId();
            List<ConfigCrmTagResp> labelList = labelGroupList.getLabelList();
            for (ConfigCrmTagResp label : labelList) {
                Long labelId = label.getLabelId();
                String labelName = label.getLabelName();
                Map<String, String> map = new HashMap<String, String>();
                Map<Long, String> mapId = new HashMap<Long, String>();
                map.put(labelGroupName, labelName);
                mapId.put(labegroupId, labelName);
                labelMap.put(labelId, map);
                labelMapId.put(labelId, mapId);
            }
        }

        //的到订单自定义标签
        SalesResponse<List<SalesFieldDTO>> resp = Sales.getSalesFieldMapping();
        List<SalesFieldDTO> listOrder = resp.getData();
        for (SalesFieldDTO order : listOrder) {
            List<SaleFieldItemDTO> fields = order.getFields();
            for (SaleFieldItemDTO field : fields) {
                log.debug(String.format("field = %1$s", field));
                //获取类型ID
                Long orderFieldId = field.getId();
                //获取类型名称
                String orderFieldName = field.getName();
                //获得具体的信息
                List<FieldParamDTO> params = field.getParams();
                if (params != null && params.size() > 0) {
                    for (FieldParamDTO param : params) {
                        if (param != null) {
                            Long id = param.getId();
                            String value = param.getValue();
                            Map<String, String> map = new HashMap<String, String>();
                            Map<Long, String> mapId = new HashMap<Long, String>();
                            map.put(orderFieldName, value);
                            mapId.put(orderFieldId, value);
                            labelMap.put(id, map);
                        }
                    }
                }
            }
        }

        //
        ConfigResponse<ConfigCustomFieldResp> resCusters = com.ec.v2.service.Config.getFieldMapping();
        List<ConfigCustomFieldResp> dataCuster = resCusters.getData();
        for (ConfigCustomFieldResp configCustomFieldResp : dataCuster) {
            log.debug(String.format("configCustomFieldResp = %1$s", configCustomFieldResp));
            Integer fieldId = configCustomFieldResp.getFieldId();
            String fieldName = configCustomFieldResp.getFieldName();
            List<ConfigCustomFieldParamResp> fieldParam = configCustomFieldResp.getFieldParam();
            for (ConfigCustomFieldParamResp configCustomFieldParamResp : fieldParam) {
                Integer paramId = configCustomFieldParamResp.getParamId();
                String paramName = configCustomFieldParamResp.getParamName();
                Map<String, String> map = new HashMap<>();
                map.put(fieldName, paramName);
                labelMap.put((long) paramId, map);
            }
        }

        return labelMap;
    }
}